### Import system modules  
import sys, string, os, arcgisscripting

### Create the Geoprocessor object
gp = arcgisscripting.create()

### Load required toolboxes...
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")

### Variables
### TEMP_LAYER takes the form '<_DB_>.<_USER_>.ENV_FACS_TEMP', e.g. 'MY_PROJECT.MY_SCHEMA.ENV_FACS_TEMP'
TEMP_LAYER = "MY_PROJECT.MY_SCHEMA.FEAT_CLASS_TEMP"
### DATABASE CONNECTIONS
# DB connections take the form 'Database Connections/CONNECTION_FILE_NAME/SDE_FEATURE_CLASS'
# e.g. 'Database Connections/LADDSDALE.MY_PROJECT.MY_SCHEMA.sde/MY_PROJECT.MY_SCHEMA.MY_FEATURE_CLASS'
# where:
#   - 'Database Connections' is a constant (a 'root' location in ArcCatalog)
#   - 'MY_SERVER.MY_PROJECT.MY_SCHEMA.sde' is the name of a SDE connection file located at
#     C:\Documents and Settings\USER_NAME\Application Data\ESRI\ArcCatalog
#     and also has the name of the DB connection under 'Database Connections' in ArcCatalog,
#     with a '.sde' suffix appended.    
#   - 'MY_PROJECT.MY_SCHEMA.MY_FEATURE_CLASS' is the fully-qualified SDE feature class name, where
#     'MY_PROJECT' is the database name, 'MY_SCHEMA' is the schema name, and 'MY_FEATURE_CLASS' is
#     the feature class/SDE layer name
###  
FEAT_CLASS_LAYER = "Database Connections/MY_SERVER.MY_PROJECT.MY_SCHEMA.sde/MY_PROJECT.MY_SCHEMA.MY_FEATURE_CLASS"
FEAT_CLASS_BACKUP = "Database Connections/MY_SERVER.MY_PROJECT.MY_SCHEMA.sde/MY_PROJECT.MY_SCHEMA.MY_FEATURE_CLASS_BAK"
FEAT_CLASS_TABLE = "Database Connections/MY_SERVER.MY_SERVER.MY_SCHEMA.sde/MY_PROJECT.MY_SCHEMA.tblFEAT_CLASS"
PROJ_CS =  "PROJCS['NAD_1983_UTM_Zone_15N',GEOGCS['GCS_North_American_1983',DATUM['D_North_American_1983',SPHEROID['GRS_1980',6378137.0,298.257222101]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Transverse_Mercator'],PARAMETER['False_Easting',500000.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',-93.0],PARAMETER['Scale_Factor',0.9996],PARAMETER['Latitude_Of_Origin',0.0],UNIT['Meter',1.0]];IsHighPrecision"

######################################### FUNCTIONS #########################################
def DeleteFeatureClass(fc):
    deleteStat = False
    print "\nDeleting feature class \n   '"  + fc + "'..."
    try:
        deleteStat = gp.Delete_management(fc, "FeatureClass")
    except Exception, e:
        print "\nERROR deleting feature class \n   '" + fc + "':"
        print e
        
    return deleteStat

def CopyFeatureClass(fcFrom, fcTo):
    print "\nCopying \n   '" + fcFrom + "'\n to \n   '" + fcTo + "'..."
    try:
        gp.CopyFeatures_management(fcFrom, fcTo, "", "0", "0", "0")
    except Exception, e:
        print "\nERROR copying features in feature class \n   '" + fcFrom + "'\n to feature class \n   '" + fcTo + "':"
        print e
        print "Exiting..."
        sys.exit(9)
    print "\nDone copying features in feature class \n   '" + fcFrom + "'\n to feature class \n   '" + fcTo + "'."

def CreateTempEventLayer(tmpTable, xCol, yCol, tmpLayer, projCS):
    print "\nCreating temp feature class \n   '" + tmpLayer + "'"
    print " from DB table \n   '" + tmpTable + "'..." 
    try:
        gp.MakeXYEventLayer_management(tmpTable, xCol, yCol, tmpLayer, projCS)
    except Exception, e:
        print "\nERROR Creating temp layer \n'" + tmpLayer + "'\n from table \n   '" + tmpTable + "'"
        print "   with MakeXYEventLayer tool:"
        print e
        print "Exiting..."
        sys.exit(9)
    print "\nDone creating temp feature class from event table."

def DeleteFieldsFromTable(fcName, fields):
    ### Note that the variable 'fields' is a python 'List'
    for field in fields:
        try:
            print "\nDeleting field '" + field + "' from feature class: \n   '" + fcName + "'..."
            gp.DeleteField_management(fcName, field)
        except Exception, e:
            print "\nERROR deleting field '" + field + "' from feature class: \n   '" + fcName + "'."
            print e
    
    
######################################### MAIN #########################################
deleteStatus = False
### Create a backup of the current feature class...
### Delete the backup table first if it already exists:
deleteStatus = DeleteFeatureClass(FEAT_CLASS_BACKUP)

if deleteStatus:
    ### Backup the feature class
    CopyFeatureClass(FEAT_CLASS_LAYER, FEAT_CLASS_BACKUP)
    ### Delete the current feature class so that a new one can be created
    deleteStatus = DeleteFeatureClass(FEAT_CLASS_LAYER)
    if deleteStatus:
        ### Create the temporary feature class/event layer from the temp table
        CreateTempEventLayer(FEAT_CLASS_TABLE, "x_coord", "y_coord", TEMP_LAYER, PROJ_CS)
        ### Copy the features in the temp layer to the 'live' feature class
        CopyFeatureClass(TEMP_LAYER, FEAT_CLASS_LAYER)
        ### Delete the 'x_coord' and 'y_coord' fields from the new feature class
        DeleteFieldsFromTable(FEAT_CLASS_LAYER, ["x_coord","y_coord"])
    else:
        print "\nERROR deleting 'live' feature class '" + FEAT_CLASS_LAYER + "'."
        print "Make sure all connections to the feature class are closed."
        print "It may be necessary to stop ArcGIS Server services to do so,"
        print "and/or to use 'sdemon' to list and kill database locks."
        print "The 'live' feature class should be restored from the backup, '" + FEAT_CLASS_BACKUP + "'."
        print "Exiting..."
        sys.exit(9)
else:
    print "\nERROR deleting backup."
    print " A feature class \n   '" + FEAT_CLASS_BACKUP + "'\n must exist first, and there can be no locks in order for this command to succeed."
    print " Exiting..."
    sys.exit(9);

print "\nCREATION OF NEW FEATURE CLASS \n   '" + FEAT_CLASS_LAYER + "'\n COMPLETE."
print " Do you need to restart ArcGIS services?"

## Added a comment here
